package com.shop.dao.impl;

import com.shop.dao.IAddressDao;
import com.shop.entity.Address;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressDaoImpl extends BaseDao implements IAddressDao {

    private static Map<String,String> map = new HashMap<>();

    static {
        map.put("a_id","aid");
        map.put("u_id","uid");
        map.put("a_name","aname");
        map.put("a_phone","aphone");
        map.put("a_detail","adetail");
        map.put("a_state","astate");
    }

    /**
     * 通过uid查询所有用户收货地址
     * @param uid
     * @return
     */
    @Override
    public List<Address> findAddressByUid(int uid) {
        String sql = "select a_id,u_id,a_name,a_phone,a_detail,a_state from address where u_id = ?";
        return queryForList(Address.class,sql,map,uid);
    }

    /**
     * 更新收货地址
     * @param address
     * @return
     */
    @Override
    public int update(Address address) {
        String sql = "update address set a_name=?,a_phone=?,a_detail=?,a_state=? where a_id=?";
        return update(sql,address.getAname(),address.getAphone(),address.getAdetail(),address.getAstate(),address.getAid());
    }

    @Override
    public int add(Address address) {
        String sql = "insert into address values(null,?,?,?,?,?)";
        return update(sql,address.getUid(),address.getAname(),address.getAphone(),address.getAdetail(),address.getAstate());
    }

    @Override
    public int delete(int aid) {
        String sql = "delete from address where a_id = ?";
        return update(sql,aid);
    }

    /**
     * 设置为默认地址
     * @param aid
     * @param i
     * @return
     */
    @Override
    public int setDefault(int aid, int i) {
        String sql = "update address set a_state = ? where a_id = ?";
        return update(sql,i,aid);
    }

    /**
     * 通过aid差寻收货地址
     * @param aid
     * @return
     */
    @Override
    public Address findAddressByAid(int aid) {
        String sql = "select a_id,u_id,a_name,a_phone,a_detail,a_state from address where a_id = ?";
        return queryForOne(Address.class,sql,map,aid);
    }

}
